/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.sml.SEFM.DAO;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.RollbackException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.SystemException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import vn.com.sml.SEFM.domain.Userstatus;
import vn.com.sml.SEFM.util.HibernateUtil;
/**
 *
 * @author Tunp
 */
public class UserStatusDAO {
      public static Integer saveUserstatus(Userstatus userstatus) throws SecurityException, RollbackException, HeuristicRollbackException, HeuristicMixedException, IllegalStateException, SystemException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Integer userstatusId = null;
        try {
            transaction = (Transaction) session.beginTransaction();
            
            userstatusId = (Integer) session.save(userstatus);
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return userstatusId;
    }
    
    public static List<Userstatus> listUserstatus() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<Userstatus> lst = new ArrayList<Userstatus>();
        try {
            transaction = (Transaction) session.beginTransaction();
            
            List lstUserstatus = session.createQuery("from Userstatus").list();
            for (Iterator iterator = (Iterator) lstUserstatus.iterator();iterator.hasNext();)
            {
                Userstatus Userstatus = (Userstatus) iterator.next();
                
                lst.add(Userstatus);
            }
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return lst;
    }
}
